package com.example.library.myRecyclerview;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.example.library.R;

import static com.example.library.utils.DensityUtils.dp2px;

/**
 * Created by dadong on 2018/9/12.
 * Describe:
 */
public class AttributeDecoration extends RecyclerView.ItemDecoration {

    private static final String TAG = "SectionDecoration";

    private DecorationCallback callback;
    private TextPaint textPaint;
    private int topGap;
    private Paint.FontMetrics fontMetrics;
    Context mContext;

    public AttributeDecoration(Context context, DecorationCallback decorationCallback) {
        this.callback = decorationCallback;
        textPaint = new TextPaint();
        textPaint.setTypeface(Typeface.SANS_SERIF);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(dp2px(context, 20));

        textPaint.setColor(context.getResources().getColor(R.color.black));
        textPaint.getFontMetrics(fontMetrics);
        textPaint.setTextAlign(Paint.Align.LEFT);
        fontMetrics = new Paint.FontMetrics();
        topGap =  dp2px(context, 30);
        mContext = context;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int pos = parent.getChildAdapterPosition(view);
        Log.i(TAG, "getItemOffsets：" + pos);
        String groupId = callback.getGroupId(pos);
        if (TextUtils.isEmpty(groupId)) return;
        if (pos == 0 || isFirstInGroup(pos)) {//同组的第一个才添加padding
            outRect.top = topGap;
        } else {
            outRect.top = 0;
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int left = parent.getPaddingLeft();
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(view);
            String groupId = callback.getGroupId(position);
            if (TextUtils.isEmpty(groupId)) return;
            String textLine = callback.getGroupFirstLine(position);
            if (position == 0 || isFirstInGroup(position)) {
                float bottom = view.getTop();
                c.drawText(textLine, left, bottom - dp2px(mContext, 10), textPaint);//绘制文本
            }
        }
    }

    private boolean isFirstInGroup(int pos) {
        if (pos == 0) {
            return true;
        } else {
            String prevGroupId = callback.getGroupId(pos - 1);
            String groupId = callback.getGroupId(pos);
            return !prevGroupId.equals(groupId);
        }
    }

    public interface DecorationCallback {

        String getGroupId(int position);

        String getGroupFirstLine(int position);
    }

}
